﻿package com.thaiflashdev.display
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import fl.transitions.easing.Regular;
	import fl.transitions.Tween;
	
	//import caurina.transitions.Tweener;
	
	import flash.geom.Point;
	
	/**
	 * @author	keerotic
	 */	
	dynamic public class Evader extends MovieClip
	{
		// list of evader object in stage
		static protected var evaderList:Array;
		
		// center position of evade area
		private var centerX:Number = 0;
		private var centerY:Number = 0;
		
		// radius of evade area
		public  var radius:Number = 400;
		
		// old mouse position 
		protected var oldMouseX:Number = 0;
		protected var oldMouseY:Number = 0;
		
		// for refer to tween object
		private var tweenX:Tween;
		private var tweenY:Tween;
		
		/**
		 * constructor
		 * 
		 */		
		public function Evader()
		{
			if(!Evader.evaderList) Evader.evaderList = [];

			mark(x,y);
			addEventListener(Event.ADDED_TO_STAGE,addToStage);
			addEventListener(Event.REMOVED_FROM_STAGE,removeFromStage);
		}
		
		/**
		 * Mark center point of evade area 
		 * @param x
		 * @param y
		 */		
		public function mark(x:Number,y:Number):void
		{
			centerX = x;
			centerY = y;
		}
		
		/**
		 * Evading from mouse
		 * @param e
		 */		
		protected function evade(e:Event):void
		{
			// don't evade if disabled
			if( ! super.enabled) 
				return;
				
			// don't evade if not moved
			if(oldMouseX == parent.mouseX && oldMouseY == parent.mouseY)
				return;
			
			// stop tween
			if(tweenX) tweenX.stop();
			if(tweenY) tweenY.stop();
			
			// update mouse position
			oldMouseX = parent.mouseX;
			oldMouseY = parent.mouseY;
			
			// find distance and angle
			var deltaX:Number = oldMouseX-x;
			var deltaY:Number = oldMouseY-y;
			var distance:Number = Point.distance(new Point(oldMouseX,oldMouseY),new Point(centerX,centerY));
			var angle:Number = Math.atan2(deltaY,deltaX)+Math.PI;
			
			
			// evade to opposite of mouse position by center position
			if(distance < radius)
			{
				var targetX:Number = centerX+(radius-distance)/10*Math.cos(angle);
				var targetY:Number = centerY+(radius-distance)/10*Math.sin(angle);
				
				//Tweener.addTween(this,{x:targetX,y:targetY,time:1});
				tweenX = new Tween(this,"x",Regular.easeOut,x,targetX,10);
				tweenY = new Tween(this,"y",Regular.easeOut,y,targetY,10);
			}
			// go back to center position
			else
			{
				//Tweener.addTween(this,{x:centerX,y:centerY,time:1});
				tweenX = new Tween(this,"x",Regular.easeOut,x,centerX,10);
				tweenY = new Tween(this,"y",Regular.easeOut,y,centerY,10);
			}
		}
		
		/**
		 * Set evader object enabled
		 * @param value
		 */		
		public override function set enabled(value:Boolean):void
		{
			super.enabled = value;
			
			if( ! value)
			{
				if(tweenX) tweenX.stop();
				if(tweenY) tweenY.stop();
				tweenX = new Tween(this,"x",Regular.easeOut,x,centerX,10);
				tweenY = new Tween(this,"y",Regular.easeOut,y,centerY,10);
			}
		}
		
		/**
		 * Get is evader object enabled?
		 * @return is evader object enabled?
		 */		
		public override function get enabled():Boolean
		{
			return super.enabled;
		}

		/**
		 * Add evade action when added to stage
		 * @param e
		 */		
		private function addToStage(e:Event):void
		{
			addEventListener(Event.ENTER_FRAME,evade);
			Evader.evaderList.push(this); // collect evader object
		}

		/**
		 * Remove evade action when removed to stage
		 * @param e
		 */		
		private function removeFromStage(e:Event):void
		{
			removeEventListener(Event.ENTER_FRAME,evade);
			Evader.evaderList.splice(Evader.evaderList.lastIndexOf(this),1); // remove evader object from collection
		}
		
	}
}